package com.apobates.jforum2.troll.actionlog.biz.impl;

import com.apobates.jforum2.troll.actionlog.biz.BoardActionCollectionService;
import com.apobates.jforum2.troll.actionlog.biz.dao.BoardActionCollectionDao;
import com.apobates.jforum2.troll.actionlog.entity.BoardActionCollection;
import com.apobates.jforum2.troll.regular.ForumActionEnum;
import com.apobates.jforum2.troll.utils.persistence.Page;
import com.apobates.jforum2.troll.utils.persistence.Pageable;
import java.util.Optional;
import java.util.stream.Stream;

import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 *
 * @author xiaofanku
 * @since 20200514
 */
@Service
public class BoardActionCollectionServiceImpl implements BoardActionCollectionService{
    @Autowired
    private BoardActionCollectionDao boardActionCollectionDao;
    private final static Logger logger = LoggerFactory.getLogger(BoardActionCollectionServiceImpl.class);

    @Override
    public Page<BoardActionCollection> getByMember(long memberId, Pageable pageable) {
        return boardActionCollectionDao.findAllByMember(memberId, pageable);
    }
    
    @Override
    public Page<BoardActionCollection> getByBoard(long boardId, Pageable pageable) {
        return boardActionCollectionDao.findAllByBoard(boardId, pageable);
    }
    
    @Override
    public Page<BoardActionCollection> getByBoard(long boardId, ForumActionEnum action, Pageable pageable) {
        return boardActionCollectionDao.findAllByBoard(boardId, action, pageable);
    }
    
    @Override
    public Page<BoardActionCollection> getAll(Pageable pageable) {
        return boardActionCollectionDao.findAll(pageable);
    }
    
    @Override
    public Stream<BoardActionCollection> getRecentByMember(long memberId, int size) {
        return boardActionCollectionDao.findRecentByMember(memberId, size);
    }
    
    @Override
    public Stream<BoardActionCollection> getMemberFavoriteBoard(long memberId, int size) {
        return boardActionCollectionDao.findAllMemberFavoriteBoard(memberId, size);
    }

    @Transactional
    @GlobalTransactional
    @Override
    public void create(BoardActionCollection entity) {
        boardActionCollectionDao.save(entity);
    }
    
    @Override
    public Optional<Boolean> isFavoritedBoard(long boardId, long memberId) {
        return boardActionCollectionDao.isFavorited(boardId, memberId);
    }
    
    @Override
    public long countMemberBoardAction(long memberId, long boardId, ForumActionEnum action) {
        return boardActionCollectionDao.countMemberAction(memberId, boardId, action);
    }
    //从版块收藏/取消收藏中脱离出来
    @Transactional
    @Override
    public boolean remove(long boardId, long memberId, ForumActionEnum action) {
        boardActionCollectionDao.detele(boardId, memberId, action);
        return true;
    }
    
    @Override
    public long countFavoritesForMember(long memberId) {
        return boardActionCollectionDao.findAllByMemberCount(memberId, ForumActionEnum.BOARD_FAVORITE);
    }

    @Override
    public Page<BoardActionCollection> getFavoritesForMember(long memberId, Pageable pageable) {
        return boardActionCollectionDao.findAllByMember(memberId, ForumActionEnum.BOARD_FAVORITE, pageable);
    }
}